﻿using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Wimm.Common.Entities;
using Wimm.Common.Entities.Utils;
using Wimm.Common.Interfaces.Utils;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;

// The Templated Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234235

namespace Wimm.CustomContols
{
    public class ExpenseDataGrid : GenericDataGridControl<Expense, ExpenseIcon>
    {
        #region ItemTemplateSelector
        public DataGridTemplateSelector ItemTemplateSelector
        {
            get { return (DataGridTemplateSelector)GetValue(ItemTemplateSelectorProperty); }
            set
            {
                SetValue(ItemTemplateSelectorProperty, value);
            }
        }

        public static readonly DependencyProperty ItemTemplateSelectorProperty =
            DependencyProperty.Register("ItemTemplateSelector", typeof(DataGridTemplateSelector),
            typeof(GenericDataGridControl<Expense, ExpenseIcon>), new PropertyMetadata(null));

        #endregion

        public ExpenseDataGrid()
        {
            this.HorizontalContentAlignment = Windows.UI.Xaml.HorizontalAlignment.Stretch;
            this.VerticalContentAlignment   = Windows.UI.Xaml.VerticalAlignment.Stretch;

            this.DefaultStyleKey = typeof(ExpenseDataGrid);

            ApplyTemplate();
            Messenger.Default.Register<ExpenseUpdateGridItemMessenge>(this, UpdateGridExecute);
            Messenger.Default.Register<RefreshExpenseGrid>(this, RefreshExpenseGridExecute);

        }

        private void RefreshExpenseGridExecute(RefreshExpenseGrid obj)
        {
            this.PagedGridData.ItemsList.Clear();
            this.currentPage = 0;
            this.newItemsExists = true;
            this.ScrollViewer_Loaded(this.Sender, new RoutedEventArgs());
        }

        private void UpdateGridExecute(ExpenseUpdateGridItemMessenge itemToUpdate)
        {
            var item = this.PagedGridData.ItemsList.FirstOrDefault(x => x.Id == itemToUpdate.ItemId);
            var itemIndex = this.PagedGridData.ItemsList.IndexOf(item);
            item.Comment = itemToUpdate.Comment;
            this.PagedGridData.ItemsList.RemoveAt(itemIndex);
            System.Diagnostics.Debug.WriteLine("Comment " + item.Comment + " Id " + item.Id + " Index " + itemIndex);
            this.PagedGridData.ItemsList.Insert(itemIndex, item);
        }

        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            var contentGrid = GetTemplateChild("LayoutRootGenericList") as Grid;
            contentGrid.DataContext = this;
            var scrl = GetTemplateChild("scrl") as ScrollViewer;
            scrl.Loaded += ScrollViewLoaded;
            scrl.ViewChanged += ScrollViewChanged;
        }

        private void ScrollViewLoaded(object sender, RoutedEventArgs e)
        {
            if (this.loadedHanded)
                return;
            this.Sender = sender;

            this.PagedGridData.ItemsList.Clear();
            base.ScrollViewer_Loaded(sender, e);

            this.loadedHanded = true;
        }

        private void ScrollViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
        {
            base.ScrollViewer_ViewChanged(sender, e);
        }

        protected override IPagedRequest<Expense> GetNextPage(int currentPage, int itemsOnPage)
        {
            return new ExpenseQuery(currentPage, itemsOnPage);
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            ItemTemplate = this.ItemTemplateSelector.ItemTemplate;
            HeaderTemplate = this.ItemTemplateSelector.HeaderTemplate;

            if (availableSize.Width <= 500)
            {
                ItemTemplate = this.ItemTemplateSelector.SmallItemTemplate;
                HeaderTemplate = this.ItemTemplateSelector.SmallHeaderTemplate;
            }

            return base.MeasureOverride(availableSize);
        }

    }
}
